<html>
<head><meta charset="utf-8"><title>span context for macro expansion · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html">span context for macro expansion</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="248648961"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/span%20context%20for%20macro%20expansion/near/248648961" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html#248648961">(Aug 06 2021 at 18:22)</a>:</h4>
<p>If a proc macro attaches an input span to the tokens it produces, should that span still be in the root context?</p>



<a name="248648993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/span%20context%20for%20macro%20expansion/near/248648993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html#248648993">(Aug 06 2021 at 18:22)</a>:</h4>
<p>let's assume there are no nested invocations</p>



<a name="248667634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/span%20context%20for%20macro%20expansion/near/248667634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html#248667634">(Aug 06 2021 at 20:58)</a>:</h4>
<p>If the macro copies the span from some input token, then yes, it will have the root context.</p>



<a name="248676763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/span%20context%20for%20macro%20expansion/near/248676763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html#248676763">(Aug 06 2021 at 22:40)</a>:</h4>
<p>I guess there's not an easy way to check if the span has "gone through" a proc macro then?</p>



<a name="248676840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/span%20context%20for%20macro%20expansion/near/248676840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html#248676840">(Aug 06 2021 at 22:41)</a>:</h4>
<p>we check for root context-ness when producing some diagnostic suggestions at the module item level, which leads to <a href="https://github.com/rust-lang/rust/issues/87613">#87613</a></p>



<a name="248677639"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/span%20context%20for%20macro%20expansion/near/248677639" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html#248677639">(Aug 06 2021 at 22:53)</a>:</h4>
<p>hmm, but the whole <em>item</em> span is in the root context, how does that work?</p>



<a name="248677776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/span%20context%20for%20macro%20expansion/near/248677776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html#248677776">(Aug 06 2021 at 22:55)</a>:</h4>
<p>(the item is the <code>fn main()</code> in this case)</p>



<a name="248677865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/span%20context%20for%20macro%20expansion/near/248677865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html#248677865">(Aug 06 2021 at 22:56)</a>:</h4>
<p>the fully expanded function definitely contains code that is generated by the proc macro, but for diagnostics purposes we usually want to consider the function to be defined in the root context</p>



<a name="248677875"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/span%20context%20for%20macro%20expansion/near/248677875" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html#248677875">(Aug 06 2021 at 22:56)</a>:</h4>
<p>There are two posibilities:<br>
1) Tokio modifies the <code>SyntaxContext</code> (via <code>Span::located_at</code>) of the code it generates, so that all of the emitted code has the root context.<br>
2) The emitted token stream has a mixture of spans - when rust tries to join the spans during parsing, it ends up throwing away the non-root <code>SyntaxContext</code>, using the root syntax context for the overall span</p>



<a name="248677895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/span%20context%20for%20macro%20expansion/near/248677895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html#248677895">(Aug 06 2021 at 22:56)</a>:</h4>
<p>I suspect that it's the second one</p>



<a name="248678128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/span%20context%20for%20macro%20expansion/near/248678128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html#248678128">(Aug 06 2021 at 22:59)</a>:</h4>
<p>Proc-macros are powerfull enough that any diagnostic / suggestion that we right can be 'broken' in some way by a proc macro</p>



<a name="248678181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/span%20context%20for%20macro%20expansion/near/248678181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/span.20context.20for.20macro.20expansion.html#248678181">(Aug 06 2021 at 23:00)</a>:</h4>
<p>To get nice error messages, a proc macro needs to 'cooperate' in some way (at a minimum, not give crazy root-context spans to everything)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>